/**
 * 
 */
package com.ezops.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author admin
 * 
 */
public class FileConverterUtil {
	final static Logger logger = LoggerFactory.getLogger(FileConverterUtil.class);
//	public static final int CELL_TYPE_STRING = 1;
//	public static final int CELL_TYPE_FORMULA = 2;
//	public static final int CELL_TYPE_BLANK = 3;
//	public static final int CELL_TYPE_BOOLEAN = 4;
//	public static final int CELL_TYPE_ERROR = 5;

	/**
	 * Convert input xsl to csv file
	 * @param inputFile
	 * @param outputFile
	 */
	public static void convertXLSXToCSV(File inputFile, File outputFile) {
//		FileOutputStream fos = null;
//		XSSFWorkbook wBook = null;
//		XSSFSheet sheet = null;
//		StringBuffer csvDataBuffer = null;
//
//		try {
//			// For storing data into CSV files
//			csvDataBuffer = new StringBuffer();
//			fos = new FileOutputStream(outputFile);
//			// Get the workbook object for XLSX file
//			wBook = new XSSFWorkbook(new FileInputStream(inputFile));
//			int totalSheetCount = 0;
//			if (wBook != null) {
//				totalSheetCount = wBook.getNumberOfSheets();
//				// System.out.println("Total sheets in XSl file : " +
//				// wBook.getNumberOfSheets());
//			}
//
//			if (totalSheetCount > 0) {
//				// Iterate through loop and read all sheets one by one
//				// for(int i=0 ; i<totalSheetCount ;i++){
//				// Get first sheet from the workbook
//				sheet = wBook.getSheetAt(0); // i
//				// System.out.println("Tab Name " + sheet.getSheetName());
//				Row row;
//				//Cell cell;
//				// Iterate through each rows from first sheet
//				Iterator<Row> rowIterator = sheet.iterator();
//				while (rowIterator.hasNext()) {
//					row = rowIterator.next();
//					// For each row, iterate through each columns
//					seperateCellData(csvDataBuffer, row);
//				}
//				fos.write(csvDataBuffer.toString().getBytes());
//				// }
//				fos.close();
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		} finally {
//
//		}
//	}
//
//	protected static void seperateCellData(StringBuffer data, Row row) {
//		Cell cell;
//		Iterator<Cell> cellIterator = row.cellIterator();
//		while (cellIterator.hasNext()) {
//			cell = cellIterator.next();
//			switch (cell.getCellType()) {
//			case Cell.CELL_TYPE_BOOLEAN:
//				data.append(cell.getBooleanCellValue() + ",");
//				break;
//			case Cell.CELL_TYPE_NUMERIC:
//				data.append(cell.getNumericCellValue() + ",");
//				break;
//			case Cell.CELL_TYPE_STRING:
//				data.append(cell.getStringCellValue() + ",");
//				break;
//			case Cell.CELL_TYPE_BLANK:
//				data.append("" + ",");
//				break;
//			default:
//				data.append(cell + ",");
//			}
//		}
//		data.append("\r\n");
	}

	public static void main(String[] args) {
		try {
			File inputFile = new File("D:/Test Data/aging_report.xlsx");
			// writing excel data to csv
			File outputFile = new File("D:/Test Data/aging_report11235.csv");
			convertXLSXToCSV(inputFile, outputFile);

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
